using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using JGSY.CMS.LowCode.Platform.Application.DTOs;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Controllers
{
    /// <summary>
    /// 角色管理接口。
    /// 提供角色的创建、查询、更新、删除等相关功能的 API。
    /// </summary>
    [ApiController]
    [Route("api/roles")]
    [Authorize(Roles = "admin")]
    public class RoleController : ControllerBase
    {
        /// <summary>
        /// 角色应用服务接口
        /// </summary>
        private readonly IRoleAppService _service;
        
        /// <summary>
        /// 构造函数，注入角色服务
        /// </summary>
        /// <param name="service">角色应用服务接口</param>
        public RoleController(IRoleAppService service)
        {
            _service = service;
        }
        /// <summary>
        /// 获取所有角色列表
        /// </summary>
        /// <returns>角色列表</returns>
        [HttpGet]
        public async Task<IEnumerable<RoleDto>> GetAll() => await _service.GetAllAsync();
        
        /// <summary>
        /// 根据ID获取指定角色信息
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <returns>角色信息，如果未找到返回404</returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<RoleDto>> GetById(long id)
        {
            var result = await _service.GetByIdAsync(id);
            if (result == null) return NotFound();
            return result;
        }
        /// <summary>
        /// 添加新角色
        /// </summary>
        /// <param name="dto">角色数据传输对象</param>
        /// <returns>添加结果</returns>
        [HttpPost]
        public async Task<IActionResult> Add(RoleDto dto)
        {
            await _service.AddAsync(dto);
            return Ok();
        }
        /// <summary>
        /// 更新指定角色信息
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <param name="dto">角色数据传输对象</param>
        /// <returns>更新结果</returns>
        [HttpPut("{id}")]
        public async Task<IActionResult> Update(long id, RoleDto dto)
        {
            await _service.UpdateAsync(id, dto);
            return Ok();
        }
        /// <summary>
        /// 删除指定角色
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(long id)
        {
            await _service.DeleteAsync(id);
            return Ok();
        }
        
        /// <summary>
        /// 为指定角色分配权限
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <param name="permissionIds">权限ID列表</param>
        /// <returns>分配结果</returns>
        [HttpPost("{id}/permissions")]
        public async Task<IActionResult> AssignPermissions(long id, List<string> permissionIds)
        {
            await _service.AssignPermissionsAsync(id, permissionIds);
            return Ok();
        }
        
        /// <summary>
        /// 获取指定角色的权限列表
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <returns>权限列表</returns>
        [HttpGet("{id}/permissions")]
        public async Task<IEnumerable<PermissionDto>> GetPermissions(long id)
            => await _service.GetPermissionsAsync(id);
    }
}
